package com.xjy.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.xjy.common.core.page.PagePlus;
import com.xjy.common.core.page.TableDataInfo;
import com.xjy.common.utils.PageUtils;

import com.xjy.system.domain.bo.SysImageTypeAddBo;
import com.xjy.system.domain.bo.SysImageTypeEditBo;
import com.xjy.system.domain.bo.SysImageTypeQueryBo;
import com.xjy.system.domain.vo.SysImageTypeVo;
import com.xjy.system.service.ISysImageTypeService;
import org.springframework.stereotype.Service;
import com.xjy.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.xjy.system.domain.SysImageType;
import com.xjy.system.mapper.SysImageTypeMapper;


import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 影像类别信息Service业务层处理
 *
 * @author zc
 * @date 2022-02-07
 */
@Service
public class SysImageTypeServiceImpl extends ServicePlusImpl<SysImageTypeMapper, SysImageType> implements ISysImageTypeService {

    @Override
    public SysImageTypeVo queryById(Long id){
        return getVoById(id, SysImageTypeVo.class);
    }

    @Override
    public TableDataInfo<SysImageTypeVo> queryPageList(SysImageTypeQueryBo bo) {
        PagePlus<SysImageType, SysImageTypeVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), SysImageTypeVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<SysImageTypeVo> queryList(SysImageTypeQueryBo bo) {
        return listVo(buildQueryWrapper(bo), SysImageTypeVo.class);
    }

    private LambdaQueryWrapper<SysImageType> buildQueryWrapper(SysImageTypeQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysImageType> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTypeNo() != null, SysImageType::getTypeNo, bo.getTypeNo());
        lqw.like(StrUtil.isNotBlank(bo.getName()), SysImageType::getName, bo.getName());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(SysImageTypeAddBo bo) {
        SysImageType add = BeanUtil.toBean(bo, SysImageType.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(SysImageTypeEditBo bo) {
        SysImageType update = BeanUtil.toBean(bo, SysImageType.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(SysImageType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
